﻿using ZyGames.Framework.Game.Contract;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 修改昵称
	/// </summary>
	public class Action10062 : BaseAction
	{
		private int userId;
		private string nickName;
		private bool result = false;
		private string log = "";
		public Action10062(HttpGet actionGetter)
			: base((short)ActionType.DZEditNickName, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("UserId", ref userId) &&
				httpGet.GetString("NickName", ref nickName))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			var currentGameUser = GameTable.Current.GetUser(userId);
			if (currentGameUser == null)
			{
				Tool.WriteWarmingLine("消息10062玩家不存在");
				result = false;
				return true;
			}
			if(string.Equals(currentGameUser.NickName, nickName))
			{
				Tool.WriteWarmingLine("消息10062玩家不存在");
				log = "不能跟之前的昵称相同";
				result = false;
				return true;
			}
			if (!CheckLengthLegal(nickName))
			{
				Tool.WriteErrorLine("消息10062名字不符合长度要求");
				result = false;
				log = "名字不合法";
				return true;
			}

			result = true;
			return true;
		}

		private bool CheckLengthLegal(string nickName)
		{
			if (string.IsNullOrEmpty(nickName))
				return false;
			var length = System.Text.Encoding.Default.GetBytes(nickName).Length;
			if (length <= 0 || length > 16)
				return false;
			else
				return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(this.result);
			this.PushIntoStack(this.log);
		}
	}
}
